package com.sg.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.sg.common.ResponseInfo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
//@CrossOrigin(origins = "*", maxAge = 3600)
public class AIController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/ai")
    public ResponseInfo<String> ai(String message){
        // 判断当前是否正在会话
//        String s = stringRedisTemplate.opsForValue().get("ai");
//        if(StringUtils.hasLength(s)){
//            return ResponseInfo.error("佛肚树当前正在对话");
//        }
        String url = "https://api.moonshot.cn/v1/chat/completions";
        Map<String, String> map = new HashMap<>();
        map.put("Content-Type","application/json");
        map.put("Authorization","Bearer sk-8deQZTgbwFx5RfHsdLkEhpP1tsn1ayRDN0cTslnj0ABi6Htp");
        JSONObject object = getEntries(message);
        String body = HttpUtil.createPost(url).addHeaders(map).body(object.toString()).execute().body();
        stringRedisTemplate.opsForValue().set("ai", body);
        if(!StringUtils.hasLength(body)){
            return ResponseInfo.error("暂无对话");
        }
        JSONObject jsonObject = JSON.parseObject(body);
        JSONArray choices = jsonObject.getJSONArray("choices");
        if(choices.isEmpty()){
            return ResponseInfo.error("暂无对话");
        }
        JSONObject jsonObject1 = choices.getJSONObject(0);
        JSONObject message1 = jsonObject1.getJSONObject("message");
        if(message == null){
            return ResponseInfo.error("暂无对话");
        }
        String string = message1.getString("content");
        string = string.replaceAll("\\n","").replaceAll("\\*","");
        return ResponseInfo.success(string);
    }


    @GetMapping("getAI")
    public ResponseInfo<String> getAI(){
        String s = stringRedisTemplate.opsForValue().get("ai");
        if(!StringUtils.hasLength(s)){
            return ResponseInfo.error("暂无对话");
        }
        JSONObject jsonObject = JSON.parseObject(s);
        JSONArray choices = jsonObject.getJSONArray("choices");
        if(choices.isEmpty()){
            return ResponseInfo.error("暂无对话");
        }
        JSONObject jsonObject1 = choices.getJSONObject(0);
        JSONObject message = jsonObject1.getJSONObject("message");
        if(message == null){
            return ResponseInfo.error("暂无对话");
        }
        String string = message.getString("content");
        string = string.replaceAll("\\n","").replaceAll("\\*","");
        return ResponseInfo.success(string);
    }


    @GetMapping("/finished")
    public ResponseInfo<Boolean> finished(){
        stringRedisTemplate.delete("ai");
        return ResponseInfo.success(true);
    }

    private static JSONObject getEntries(String message) {
        JSONObject object = new JSONObject();
        object.put("model","moonshot-v1-8k");
        JSONArray objects = new JSONArray();
        JSONObject message1 = new JSONObject();
        message1.put("role","system");
        message1.put("content","你叫佛肚树，是一位充满智慧的老人，上知天文，下知地理，你可以以一位长者的语气为用户提供安全，有帮助，准确的回答，同时，你会拒绝一切涉及恐怖主义，种族歧视，黄色暴力等问题的回答");
        objects.add(message1);
        JSONObject message2 = new JSONObject();
        message2.put("role","user");
        message2.put("content", message);
        objects.add(message2);
        object.put("messages",objects);
        object.put("temperature",0.3);
        return object;
    }
}
